package com.gj.test;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Map;
import java.util.regex.Pattern;

import javax.websocket.Session;

import com.google.common.collect.Maps;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType.LaunchOptions;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.StrFormatter;

import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.Route;


public class InterceptNetworkRequests {
  public static void main(String[] args) throws Exception { 
	  //System.setProperty("PLAYWRIGHT_SKIP_BROWSER_GC","1"); 
	  
    try (Playwright playwright = Playwright.create()) {
	  ArrayList<String> arrayList = new ArrayList<>(); 
	  arrayList.add("--no-sandbox");
      arrayList.add("--disable-setuid-sandbox");
	  LaunchOptions options = new LaunchOptions()/*.setDownloadsPath(Paths.get("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"))*/.setArgs(arrayList).setHeadless(false);    
      Browser browser = playwright.chromium().launch(options);
      /*BrowserContext context = browser.newContext();
      Page page = context.newPage(); */
       
      startRoomPage(browser.newPage(),"332702617840");
      Thread.currentThread().join();
    }
  }
  public static void startRoomPage(Page page,String RoomId) throws Exception{
  	page.setDefaultNavigationTimeout(0); 
  	page.navigate(StrFormatter.format("https://live.douyin.com/{}", RoomId));
  	//page.setRequestInterception(true);
  	//page.setCacheEnabled(false);  
  	 
  	 page.exposeFunction("sendRoomMesssage", args -> {
  		 	System.out.println(args[0]);
  		 	return args;
  	 });
  	
  	 page.exposeFunction("sendRoomError", args -> {
		 	System.out.println(args[0]);
		 	return args;
	 });
  	 
  	Path imageFile = Paths.get("douyin/dd.js");
  	page.route(Pattern.compile("common-utils-message*"), route -> { 
  		System.out.println("拦截关键脚本:"+ route.request().url());
         	if (route.request().url().indexOf("common-utils-message") != -1) {
             
             	Map<String,String> headers = Maps.newHashMap();
             	headers.put("Access-Control-Allow-Origin",  "*"); 
             	route.fulfill(new Route.FulfillOptions()
             		    .setStatus(200)
             		    .setHeaders(headers)
             		    .setContentType("application/javascript")
             		    .setBody(FileUtil.readString(imageFile.toString(), "utf-8")));
             	System.out.println("已替换:"+ route.request().url());
	          } else {
	        	  route.resume();
	          }
        });
  	/*page.onRequest(request -> {  
          if (request.url().toString().indexOf("common-utils-message") != -1) {
             	System.out.println("拦截关键脚本:"+ request.url());
             	Map<String,String> headers = Maps.newHashMap();
             	headers.put("Access-Control-Allow-Origin",  "*"); 
             // request.respond(200, headers, "application/javascript", FileUtil.readString(imageFile.toString(), "utf-8"));
  			System.out.println("已替换:"+ request.url());
          } else {
              request.response();
          }
      });*/
  		page.reload();
  }
}